﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.WebControls;

namespace Trig_AMJ_HR_Forms.Layouts.Trig_AMJ_HR_Forms
{
    public partial class Cancel_Leave_Request_Form : LayoutsPageBase
    {
        SPHelper obj = new SPHelper();

        #region "Properties"

        string CurrentUser = SPContext.Current.Web.CurrentUser.LoginName;

        public static string SiteURL;
        public static string TicketNo;
        public static string MainLstName;
        public static string TaskLstName;
        public static string AttLibName;
        public static string CmntsLstName;
        public static string WorkflowConfigList;
        public static string lstGuid { get; set; }
        public string camlQry { get; set; }
        public static string Mode { get; set; }

        public static string submittedfor { get; set; }

        string ErrorMsgs;

        public static int wfOrder = 0;

        public static string DocumentTypesList { get; set; }
        string WFCurrentOrder = string.Empty;
        string WFTaskType = string.Empty;

        Demand_Voucher_FormClass demandVoucherclass = new Demand_Voucher_FormClass(); // Helper class for this solution
        Trig_WF trigWFClass = new Trig_WF();

        string[] buttonVisibilty = new string[5];

        Label EmpName; Label EmpID; Label Division; Label Designation; Label MobNo;

        #endregion

        public Cancel_Leave_Request_Form()
        {
            SiteURL = SPContext.Current.Site.Url;
            SiteURL = SiteURL + "/sites/forms/";
            MainLstName = "Leave Request Forms";
            TaskLstName = "Leave Request Tasks";
            CmntsLstName = "Leave Request Comments";
            AttLibName = "Attachments";
            WorkflowConfigList = "Workflow Configuration List";
            DocumentTypesList = "DocumentTypes";

        }

        #region "User control manipulation"
        /// <summary>
        /// Get user control controls using find control method 
        /// </summary>
        private void GetUserControls()
        {
            EmpName = userInfo.FindControl("lblEmpName") as Label;
            EmpID = userInfo.FindControl("lblEmpID") as Label;
            Division = userInfo.FindControl("lblDivision") as Label;
            Designation = userInfo.FindControl("lblDesignation") as Label;
            MobNo = userInfo.FindControl("lblMobNo") as Label;
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {

            try
            {
                Mode = Request.QueryString["Mode"];
                GetUserControls();
                if (Mode == "Print" || Mode == "View")
                {
                    ReLoadDetails();
                }

            }
            catch (Exception ex)
            {
                ErrorMsgs = "Trig_AMJ_HR_Forms :: Leave Request Form" + " :: " + "Parent" + " :: " + "Page_Load";
                obj.GenerateExceptionLog(ErrorMsgs, ex.Message, "");
            }


        }

        protected void btnCancelLeave_Click(object sender, EventArgs e)
        {

        }

        #region "Reload Forms"

        public void ReLoadDetails()
        {
            try
            {
                string RefNo = Convert.ToString(Request.Params["RefID"]);

                camlQry = "<Where><Eq><FieldRef Name=\"Reference_x0020_Number\" /><Value Type=\"Text\">" + RefNo + "</Value></Eq></Where>";
                SPListItem lstDatas = obj.GetSPListItem(SiteURL, MainLstName, camlQry);
                if (lstDatas != null)
                {
                    lblLevReqNo.Text = Convert.ToString(lstDatas["Reference Number"]);
                    dtpDate.Text = Convert.ToDateTime(lstDatas["RequestDate"]).ToString("dd/MM/yyyy");

                    Label lblFormID = userInfo.FindControl("lblFormID") as Label;
                    lblFormID.Text = lstDatas.ParentList.ID.ToString();

                    submittedfor = Convert.ToString(lstDatas["SubmittedFor"]);
                    SPFieldUserValue fldUser = new SPFieldUserValue(SPContext.Current.Web, submittedfor);
                    submittedfor = fldUser.LookupValue;

                    if (Convert.ToString(lstDatas["OnBehalfof"]) == "Yes")
                    {

                        lblOnBehalf.Text = "On Behalf of";
                        rdBtn.SelectedIndex = 1;
                        pnlReqEmpInf.Visible = true;
                        SetValuestoOnBehalfof(lstDatas);
                    }

                    else
                    {
                        lblOnBehalf.Text = "Logged in User";
                        rdBtn.SelectedIndex = 0;
                    }

                    rdBtn.Enabled = false;
                    rdBtnAirTkt.Enabled = false;
                    rdBtnEligible.Enabled = false;
                    rdBtnLeaveType.Enabled = false;

                    for (int i = 0; i < rdBtnLeaveType.Items.Count; i++)
                    {
                        if (Convert.ToString(lstDatas["LeaveType"]) == rdBtnLeaveType.Items[i].ToString())
                            rdBtnLeaveType.SelectedIndex = i;
                        lblLeaveType.Text = Convert.ToString(lstDatas["LeaveType"]);

                        if (Convert.ToString(lstDatas["LeaveType"]) == "Others")
                        {
                            lblLeaveType.Text = "Others - " + Convert.ToString(lstDatas["LeaveOthers"]);
                        }
                        else
                        {
                            lblLeaveType.Text = Convert.ToString(lstDatas["LeaveType"]);

                        }
                    }

                    if (Convert.ToString(lstDatas["LeaveType"]) == "Others")
                    {
                        divLeaveOth.Visible = true;
                        txtLeaveOthers.Text = Convert.ToString(lstDatas["LeaveOthers"]);
                    }
                    else
                    {
                        divLeaveOth.Visible = false;
                        txtLeaveOthers.Text = "";
                    }

                    if (Convert.ToString(lstDatas["VacationEligibility"]) == "Yes")
                    {
                        rdBtnEligible.SelectedIndex = 0;
                        lblEligible.Text = "Yes";
                    }
                    else
                    {
                        rdBtnEligible.SelectedIndex = 1;
                        lblEligible.Text = "No";
                    }
                    if (Convert.ToString(lstDatas["AirticketBornebyEmployee"]) == "Yes")
                    {
                        rdBtnAirTkt.SelectedIndex = 0;
                        lblAirtkt.Text = "Yes";
                    }
                    else
                    {
                        rdBtnAirTkt.SelectedIndex = 1;
                        lblAirtkt.Text = "No";
                    }

                    string user = Convert.ToString(lstDatas["SubmittedFor"]);

                    //trigWFClass.submittedFor = user;

                    if (!string.IsNullOrEmpty(Convert.ToString(lstDatas["DateofEmployment"])))
                        txtDOJ.Text = Convert.ToDateTime(lstDatas["DateofEmployment"]).ToString("dd/MM/yyyy");

                    txtExpYear.Text = Convert.ToString(lstDatas["YearofExperience"]);
                    txtPassNo.Text = Convert.ToString(lstDatas["PassportNo"]);

                    if (!string.IsNullOrEmpty(Convert.ToString(lstDatas["PassportExpiryOn"])))
                        txtPassExpire.Text = Convert.ToDateTime(lstDatas["PassportExpiryOn"]).ToString("dd/MM/yyyy");
                    txtQatariID.Text = Convert.ToString(lstDatas["QatarID"]);
                    if (!string.IsNullOrEmpty(Convert.ToString(lstDatas["QatarIDExpireOn"])))
                        txtQatarIDExpire.Text = Convert.ToDateTime(lstDatas["QatarIDExpireOn"]).ToString("dd/MM/yyyy");
                    txtNationality.Text = Convert.ToString(lstDatas["Nationality"]);

                    dtTravelDt.Text = Convert.ToDateTime(lstDatas["TravelDate"]).ToString("dd/MM/yyyy");
                    //txtTravelDt.Visible = true; dtTravelDt.Visible = false;
                    //txtReturnDt.Text = Convert.ToDateTime(lstDatas["ReturnDate"]).ToString("dd/MM/yyyy");
                    dtReturnDt.Text = Convert.ToDateTime(lstDatas["ReturnDate"]).ToString("dd/MM/yyyy");

                    //txtReturnDt.Visible = true; dtReturnDt.Visible = false;
                    txtLeaveDays.Text = Convert.ToString(lstDatas["LeaveDays"]);

                    dtWorkEOD.Text = Convert.ToDateTime(lstDatas["WorkEndorsedto"]).ToString("dd/MM/yyyy");
                    LeaveHistory(submittedfor, gvVacationDetails);

                    //GetApprovedUsers(RefNo);

                }
            }
            catch (Exception ex)
            {
                ErrorMsgs = "Trig_AMJ_HR_Forms" + " :: " + "Parent" + " :: " + "ReLoadDetails";
                obj.GenerateExceptionLog(ErrorMsgs, ex.Message, "");
            }
        }

        #endregion

        private void SetValuestoOnBehalfof(SPListItem lstDatas)
        {
            SPFieldUserValue user = new SPFieldUserValue(SPContext.Current.Web, lstDatas["SubmittedFor"].ToString());
            //setPeopleEditors(user, pplReqUser);
            txtReqUser.Text = user.LookupValue.ToString();
            txtReqEmpId.Text = Convert.ToString(lstDatas["EmployeeID"]);
            txtReqDivision.Text = Convert.ToString(lstDatas["Division"]);
            txtReqDesignation.Text = Convert.ToString(lstDatas["Designation"]);
            txtReqMobileNo.Text = Convert.ToString(lstDatas["MobileNo"]);
        }

        private void LeaveHistory(string leaveUser, GridView gvVacationDetails)
        {
            string[] str = null;
            if (!string.IsNullOrEmpty(leaveUser))
                str = leaveUser.Split('\\');


            using (SPSite oSite = new SPSite(SiteURL))
            {
                using (SPWeb oWeb = oSite.OpenWeb())
                {
                    SPQuery qry = new SPQuery();
                    camlQry = "<Where><And><Eq><FieldRef Name=\"SubmittedFor\" /><Value Type=\"User\">" + leaveUser + "</Value></Eq><Eq><FieldRef Name=\"ResumptionStatus\" />" +
                        "<Value Type=\"Text\">Yes</Value></Eq></And></Where><OrderBy><FieldRef Name=\"TravalDate\" Ascending=\"True\" /></OrderBy>";
                    qry.Query = camlQry;
                    SPListItemCollection oItems = oWeb.Lists.TryGetList(MainLstName).GetItems(qry);

                    if (oItems.Count > 0)
                    {
                        pnlgvVacations.Visible = true;
                        trVacationDetails.Visible = false;
                        lblVacationDetails.Text = "";
                        gvVacationDetails.DataSource = oItems.GetDataTable();
                        gvVacationDetails.DataBind();
                    }
                    else
                    {
                        gvVacationDetails.DataSource = null;
                        gvVacationDetails.DataBind();
                        trVacationDetails.Visible = true;
                        lblVacationDetails.Text = "No previous leave records for " + str[1].ToString().ToUpper();

                    }
                }
            }

        }

    }
}
